package com.ithong.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ithong.drug.entity.Supplier;
import com.ithong.drug.service.SupplierService;
import com.ithong.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 转向供应商页面
     * @return
     */
    @RequestMapping
    public String index(){
        return "/supplier";
    }

    /**
     * 分页查询供应商列表
     */
    @RequestMapping("page")
    @ResponseBody
    public Object supplierQueryPage(String param,
                                    @RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize){
        try {
            IPage<Supplier> iPage = supplierService.selectSupplierPage(pageNum,pageSize,param);
            return ResultMapUtil.getMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 转向新增页面
     * @return
     */
    @RequestMapping("add")
    public String add(){
        return "/supplierAdd";
    }

    /**
     * 添加一个供应商
     */
    @RequestMapping("/supplierAdd")
    @ResponseBody
    public Object supplierAdd(Supplier supplier){
        try {
            supplier.setCreatetime(new Date());
            int i = supplierService.addSupplier(supplier);
            return ResultMapUtil.getMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 转向编辑页面
     * @return
     */
    @RequestMapping("edit")
    public String edit(@RequestParam(name = "id",required = true) Integer id, Model model){
        Supplier supplier = supplierService.querySupplierById(id);
        model.addAttribute("obj",supplier);
        return "/supplierAdd";
    }

    /**
     * 修改一个供应商
     */
    @RequestMapping("supplierEdit")
    @ResponseBody
    public Object supplierEdit(Supplier supplier){
        try {
            int i = supplierService.editSupplier(supplier);
            return ResultMapUtil.getMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 删除
     */
    @RequestMapping("supplierDel")
    @ResponseBody
    public Object supplierDel(Integer id){
        try {
            int i = supplierService.delSupplier(id);
            return ResultMapUtil.getMapDel(id);
        } catch (Exception e) {
            return ResultMapUtil.getMapException(e);
        }
    }

    /**
     * 返回所有供应商
     */
    @RequestMapping("supplierList")
    @ResponseBody
    public Object supplierList(){
        List<Supplier> suppliers = supplierService.querySupplierList();
        return ResultMapUtil.getMapList(suppliers);
    }

}
